package com.sctvcloud.dongqu.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.beans.FCityItem;
import com.sctvcloud.dongqu.beans.FCityNews;
import com.sctvcloud.dongqu.beans.FCityState;
import com.sctvcloud.dongqu.beans.GovItemBean;
import com.sctvcloud.dongqu.beans.NewsItem;
import com.sctvcloud.dongqu.http.AbsNetCallBack;
import com.sctvcloud.dongqu.http.NetUtils;
import com.sctvcloud.dongqu.ui.activities.GovDetailActivity;
import com.sctvcloud.dongqu.ui.adapter.BumenSubFrgAdapter;
import com.sctvcloud.dongqu.ui.util.IListShowData;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by raoweiwei on 2017/8/3.
 * 首页通用的其他列表栏目
 */

public class BumenSubFragment extends ColumnBaseFragment implements CanRefreshLayout.OnRefreshListener
         {
    @BindView(R.id.common_frg_refreshlayout)
    protected CanRefreshLayout mRefreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView mRecyclerView;
    private BumenSubFrgAdapter mAdapter;
    private int mPageNo = 0;
    private boolean mIsLoading = false;
    private int mPageAll = 0;

    private List<IListShowData> commonList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_common_frg, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initViews(View v, @Nullable Bundle savedInstanceState) {


        mRefreshLayout.setOnRefreshListener(this);
     //   mRefreshLayout.setOnLoadMoreListener(this);
        mRefreshLayout.setLoadMoreEnabled(false);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    //    mRecyclerView.addItemDecoration(new RBaseItemDecoration(1));

        GridLayoutManager mgr = new GridLayoutManager(getContext(), 4);
        mRecyclerView.setLayoutManager(mgr);

    }

    @Override
    protected boolean onFragmentShow(boolean isFirst, boolean isCallFromResume) {
        if (isFirst) {
            mRefreshLayout.autoRefresh();
        }
        return super.onFragmentShow(isFirst, isCallFromResume);
    }

//    @Override
//    public void onLoadMore() {
//        mPageNo++;
//        getData();
//    }

    @Override
    public void onRefresh() {
        if (mIsLoading) {
            return;
        }
        mPageNo = 0;
        mIsLoading = true;
        getData();
    }


    private void getData() {

        NetUtils.getNetAdapter().getStateList(getOwnerName(), channelUrl, new AbsNetCallBack<FCityItem>(FCityNews.class) {
            @Override
            public void onSuc(FCityItem fCityItem) {
                if (fCityItem != null) {
                    initData(fCityItem.getInstitutionList(),0);
                }
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                super.onError(ex, errorMsg);
                onNetFinish();
            }

            @Override
            public void onEnd() {
                super.onEnd();
                onNetFinish();
            }
        });

    }

    private void initData(List<FCityState> newsItems, int template) {
        if (mAdapter == null) {
            mAdapter = new BumenSubFrgAdapter(getActivity(), null, template).setItemInternalClick(itemClick);
            mRecyclerView.setAdapter(mAdapter);
        }
        mAdapter.setAct(isAct);
        if (mPageNo == 0) {
            mAdapter.setData(newsItems);
        } else {
            mAdapter.addDatas(newsItems);
        }
    }

    private List<NewsItem> newsItems;



    private synchronized void onNetFinish() {
        mIsLoading = false;
        mRefreshLayout.refreshComplete();
        mRefreshLayout.loadMoreComplete();
     //   mRefreshLayout.setLoadMoreEnabled(mPageNo+1 >= mPageAll ? false : true);
    }

    OnItemInternalClick itemClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {

            GovItemBean newsItem = new GovItemBean();
            FCityState fc = (FCityState) itemView.getTag(R.id.adapter_item_data_key);

            newsItem.setInstitutionId(fc.getInstitutionId());
            newsItem.setInstitutionImage(fc.getInstitutionImage());
            newsItem.setInstitutionName(fc.getInstitutionName());

            Intent intent = new Intent(getActivity(), GovDetailActivity.class);
            intent.putExtra(EX_DATA, newsItem);
            startActivity(intent);
        }
    };
}
